package sopraim.model;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;


public  class ToolsDAO {
    
    private final SessionFactory sessionFactory = buildSessionFactory();
    
    @SuppressWarnings("deprecation")
    private  SessionFactory buildSessionFactory() throws ExceptionInInitializerError {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
            System.out.println("Session factory build");
            
            return new Configuration().configure().buildSessionFactory();
        }
        catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }
    
    public  SessionFactory getSessionFactory() {
        return sessionFactory;
    }
    
    
    /**
     * Init the database
     * @param statusDAO
     * @param adminDAO
     */
    public void initDAO(StatusDAO statusDAO, AdminDAO adminDAO)  {
        try {
            
            
            
            // locations
            System.out.println("Adding Locations");
            adminDAO.addLocation("Toulouse");
            
            
            // add the first admin (pwd=admin)
            System.out.println("Adding the first admin record !");
            adminDAO.addUser("admin","admin",null, true);
            System.out.println("Done!");
            
            
            
            
        } catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }
    
}
